<?php
//include('common.php');
?>

<?php 
// 此文件用来保存所有的函数

//设置文档编码为UTF8 , 注意此函数之前不能有任何输出 $type为1时输出的是纯文本,用于调试
function set_head($type=0)
{
	if($type) header('Content-type: text/plain; charset=UTF-8');
	else header('Content-type: text/html; charset=UTF-8');
}



//连接数据库.. PDO方式连接
function mysql_connection()
{
	global $pdo;
	$dbms='mysql';
	$dbName='jiaoshiguanlixitong';
	$user='root';
	$pwd='';
	$host='localhost';
	$dsn="$dbms:host=$host;dbname=$dbName";
	try {
		$pdo=new PDO($dsn,$user,$pwd);
		//echo "PDO连接MySQL成功";
	} catch (Exception $e) {
		echo $e->getMessage()."<br>";
	}
	$pdo->query("set names utf8;");
}

// //生成uesr数据表
function mysql_make_user_database()
{

	$sql_make_user_db = "CREATE TABLE `jiaoshiguanlixitong`.`user` (`id` INT NOT NULL AUTO_INCREMENT, `user` VARCHAR(30) NOT NULL, `password` VARCHAR(100) NOT NULL, `power` INT NOT NULL ,PRIMARY KEY (`id`)) ENGINE = InnoDB";
	$result=$pdo->prepare($sql_make_user_db);
	$result->execute();
	var_dump($result->errorInfo());
}


// //加入管理员账号 root root 
function mysql_add_root_user()
{
	$sql_add_root = "INSERT INTO `jiaoshiguanlixitong`.`user` (`id`, `user`, `password`, `power`) VALUES ('id',     'root',     'root',     '1')";
	// $result=$pdo->prepare($sql_add_root);
	// $result->execute();
	$result=$pdo->exec($sql_add_root);
}





//绘制div标签,class为空输出</div>,为0输出一行<div class="row">,为数字输出<div class="span?">,否则输出<div class="?">
function div($class='')
{
	if (is_numeric($class)){
		if($class==0) echo '<div class="row">';
		else echo '<div class="span',$class,'">';
	}
	elseif($class=='') echo '</div>';
	else  echo '<div class="',$class,'">';
	echo "\n";
}

//绘制body标签,class为空输出</body>,为0输出<body>,否则输出<body class="?">
function body($class='')
{
	if($class===0) echo '<body>';
	elseif($class=='') echo '</body>';
	else  echo '<body class="',$class,'">';
	echo "\n";
}


//绘制导航栏
function html_navbar()
{

echo <<<ZZZ

<div class="navbar navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container">
	    <ul class="nav">
			<li class="active">
			    <a href="index.php">首页</a>
			</li>
			<li>
				<a href="#">个人中心</a>
			</li>
			<li>
				<a href="#">教室概览</a>
			</li>
			<li>
				<a href="#">我的课堂</a>
			</li>
			<li>
				<a href="#">链接</a>
			</li>
			<li>
				<a href="#">链接</a>
			</li>
			<li>
				<a href="#">链接</a>
			</li>
			<li>
				<a href="#">链接</a>
			</li>
			<li>
				<a href="#">链接</a>
			</li>
			<li>
				<a href="#">友情链接</a>
			</li>
			<li>
				<a href="#">关于我们</a>
			</li>
		</ul>
    </div>
  </div>
</div>

ZZZ;
	
}

//输出换行
function br()
{
	echo "<br />\n";
}

function nn()
{
	echo "\n";
}

//在首页显示新闻列表
function index_show_news()
{
	show_hero();
 	global $pdo;
	echo '<div class="page-header" style="font-family:comic sans ms,cursive;">  <h1>NEWS:</h1> </div>';
	$sql='SELECT `title`,`news_id` FROM `news` WHERE isshow =1 ORDER BY time DESC LIMIT 0 , 10';
	$result=$pdo->query($sql);
	echo '<table class="table table-striped">';
	foreach ($result as $title) {
	    // var_dump($title);
	    echo '<tr>';
	    nn();
	    echo "<td><a href=\"show_news.php?id=$title[1]\">$title[0]</a></td>";
	    nn();
	    echo '</tr>';
	    nn();
	}
	echo '</table>';
	nn();
}

//判断是否登录
function is_login()
{
	if  (@ $_SESSION['is_login']===1) return 1;
	return 0;
}



function html_foot()
{
	$html= <<<ZZZ

<footer class="footer">
	<p>
		<img src="http://www.w3.org/html/logo/badge/html5-badge-h-css3-graphics-performance-semantics.png" class="foot" alt="html5"/>
		Copyright©2013 hnust_srip team All Rights Reserved 
		<a target="_blank" href="http://wpa.qq.com/msgrd?v=3&amp;uin=978750903&amp;site=qq&amp;menu=yes">
			<img  src="http://pub.idqqimg.com/qconn/wpa/button/button_11.gif" alt="联系我们" />
		</a>
		<a href="#">回到顶端</a>
	</p>
	    <form class="form-inline center">
      	<label class="select">
	        Select Style: <select class="input-medium" id="selstyle">
	          <option value="cerulean">Cerulean</option>
	          <option value="cyborg">Cyborg</option>
	          <option value="cosmo">Cosmo</option>
	          <option value="amelia">Amelia</option>
	          <option value="simplex">Simplex</option>
	          <option value="spacelab">Spacelab</option>
	          <option value="spruce">Spruce</option>
	          <option value="superhero">Superhero</option>
	          <option value="united">United</option>
	          <option value="journal">Journal</option>
	          <option value="readable">Readable</option>
	          <option value="slate">Slate</option>
	          <option value="darkening">Darkening</option>
	          <option value="original">Original</option>
	          <option value="metro">Metro</option>
	          <option value="geo">Geo</option>
	        </select>
		</label>
    </form>
</footer>


<footer id="footer">

</footer>


ZZZ;
echo $html;
}

//此函数输出bootstrap框架的head部分,即 <head>....</head>,$title用来指定文档的标题,注意,不包含<body>.
function html_head($title)
{
	echo <<<ZZZ

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>
			{$title}
		</title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta name="description" content="">
		<meta name="author" content="wangkechun">
		
		<link href="css/bootstrap.css" rel="stylesheet" type="text/css">
		<link href="css/style.css" rel="stylesheet" type="text/css">
		<link href="css/my.css" rel="stylesheet" type="text/css">
		<!-- <script type="text/javascript" src="js/jquery.js"></script> -->
		<!-- <script type="text/javascript" src="js/bootstrap.min.js"></script> -->
		<!--[if lt IE 9]>
		<script src=”http://html5shiv.googlecode.com/svn/trunk/html5.js”></script>
		<![endif]-->
		<link rel="shortcut icon" href="img/favicon.ico">
	</head>

ZZZ;
}



?>

